/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    RackServerMenu.cs

Abstract: 

    A class to create the Addin Menu
*/
using System;
using System.Collections.Generic;
using System.Text;
using Visio = Microsoft.Office.Interop.Visio;
using Office = Microsoft.Office.Core;
using UtilityManager;

namespace VisioManager
{
    /// <summary>
    /// Class to load and unload Addin menu
    /// </summary>
    class RackServerMenu
    {
        #region Private Variables

        // Menu objects
        private Visio.UIObject uiObject;
        private Visio.Menu rackServerMenu;
        private Visio.MenuItem rackMenuItemConfigure;
        private Visio.MenuItem rackMenuItemImportData;
        private Visio.MenuItem rackMenuItemAnalyze;
        private Visio.MenuItem rackMenuItemVirtualize;
        private Visio.MenuItem rackMenuItemStatusWindow;
        private Visio.MenuItem rackMenuItemHelp;
        private Visio.MenuItem rackMenuItemAbout;

        // Toolbar Objects
        private Visio.UIObject uiToolbarObject;
        private Visio.Toolbar rackServerToolbar;

        #endregion Private Variables

        #region Methods

        /// <summary>
        /// Function to add the addin menu to Visio Document
        /// </summary>
        public void BuildMenu(Document addinDocument)
        {
            System.Text.StringBuilder addonArgs = null;

            try
            {
                // Get the custom menu or built in menus to the UIObject
                if (addinDocument.VisioDocument.CustomMenus != null)
                {
                    uiObject = addinDocument.VisioDocument.CustomMenus.Clone;
                }
                else if (addinDocument.VisioDocument.Application.CustomMenus != null)
                {
                    uiObject = addinDocument.VisioDocument.Application.CustomMenus.Clone;
                }
                else
                {
                    uiObject = addinDocument.VisioDocument.Application.BuiltInMenus.Clone;
                }
                if (uiObject == null)
                {
                    // Menu add failed
                    return;
                }
                // Get the menu sets of the visio application
                Visio.MenuSets menuSets = uiObject.MenuSets;
                // Get the item Id of the drawing object
                short itemId = (short)Visio.VisUIObjSets.visUIObjSetDrawing;
                // Get the menu set of drawing object
                Visio.MenuSet menuSet = menuSets.get_ItemAtID(itemId);
                // Get the menus from menu set
                Visio.Menus menus = menuSet.Menus;

                // Add the menu at a position & set the caption
                rackServerMenu = menus.AddAt(menus.Count - 1);
                rackServerMenu.Caption = Global.GetResourceString("MenuVirtualRack"); 

                // Set the menu items to the menu
                Visio.MenuItems menuItems = rackServerMenu.MenuItems;

                // Add the configure menu Item to the menu
                rackMenuItemConfigure = menuItems.Add();
                rackMenuItemConfigure.Caption = Global.GetResourceString("MenuConfigure"); 
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringConfigure);
                rackMenuItemConfigure.AddOnName = "QueueMarkerEvent";
                rackMenuItemConfigure.AddOnArgs = addonArgs.ToString();
              
                // Add Import data menu item
                rackMenuItemImportData = menuItems.Add();
                rackMenuItemImportData.Caption = Global.GetResourceString("MenuImportData"); 
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringImportData);
                rackMenuItemImportData.AddOnName = "QueueMarkerEvent";
                rackMenuItemImportData.AddOnArgs = addonArgs.ToString();

                // Add the analyze menu Item to the menu
                rackMenuItemAnalyze = menuItems.Add();
                rackMenuItemAnalyze.Caption = Global.GetResourceString("MenuAnalyze");
                rackMenuItemAnalyze.State = (short)Visio.VisUIButtonState.visButtonUp;
                rackMenuItemAnalyze.BeginGroup = true;
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringAnalyze);
                rackMenuItemAnalyze.AddOnName = "QueueMarkerEvent";
                rackMenuItemAnalyze.AddOnArgs = addonArgs.ToString();

                // Add the virtualize Rack menu Item to the menu
                rackMenuItemVirtualize = menuItems.Add();
                rackMenuItemVirtualize.Caption = Global.GetResourceString("MenuVirtualize");
                rackMenuItemVirtualize.State = (short)Visio.VisUIButtonState.visButtonUp;
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringVirtualize);
                rackMenuItemVirtualize.AddOnName = "QueueMarkerEvent";
                rackMenuItemVirtualize.AddOnArgs = addonArgs.ToString();

                //// Add the virtualize Colo menu Item to the menu
                //rackMenuItemVirtualize = menuItems.Add();
                //rackMenuItemVirtualize.Caption = Global.GetResourceString("MenuVirtualizeColo");
                //rackMenuItemVirtualize.State = (short)Visio.VisUIButtonState.visButtonUp;
                //addonArgs = new System.Text.StringBuilder();
                //addonArgs.Append(Global.DocumentContextString);
                //addonArgs.Append(addinDocument.VisioDocument.ID);
                //addonArgs.Append(" ");
                //addonArgs.Append(Global.ApplicationContextString);
                //addonArgs.Append(Global.ApplicationId);
                //addonArgs.Append(" ");
                //addonArgs.Append(Global.CommandContextString);
                //addonArgs.Append(Global.CommandStringVirtualizeColo);
                //rackMenuItemVirtualize.AddOnName = "QueueMarkerEvent";
                //rackMenuItemVirtualize.AddOnArgs = addonArgs.ToString();

                //// Add the virtualize Colo menu Item to the menu
                //rackMenuItemVirtualize = menuItems.Add();
                //rackMenuItemVirtualize.Caption = Global.GetResourceString("MenuVirtualizeColo");
                //rackMenuItemVirtualize.BeginGroup = true;

                ////Add Start & Stop sub menu item
                //Visio.MenuItems coloItems = rackMenuItemVirtualize.MenuItems;
                //rackMenuItemVirtualize = coloItems.Add();
                //rackMenuItemVirtualize.Caption = Global.GetResourceString("Rack_MenuItem_Title");
                //addonArgs = new System.Text.StringBuilder();
                //addonArgs.Append(Global.DocumentContextString);
                //addonArgs.Append(addinDocument.VisioDocument.ID);
                //addonArgs.Append(" ");
                //addonArgs.Append(Global.ApplicationContextString);
                //addonArgs.Append(Global.ApplicationId);
                //addonArgs.Append(" ");
                //addonArgs.Append(Global.CommandContextString);
                //addonArgs.Append(Global.CommandStringRackVirtualize);
                //rackMenuItemVirtualize.AddOnName = "QueueMarkerEvent";
                //rackMenuItemVirtualize.AddOnArgs = addonArgs.ToString();

                //rackMenuItemVirtualize = coloItems.Add();
                //rackMenuItemVirtualize.Caption = Global.GetResourceString("RackAndServer_MenuItem_Title");
                //addonArgs = new System.Text.StringBuilder();
                //addonArgs.Append(Global.DocumentContextString);
                //addonArgs.Append(addinDocument.VisioDocument.ID);
                //addonArgs.Append(" ");
                //addonArgs.Append(Global.ApplicationContextString);
                //addonArgs.Append(Global.ApplicationId);
                //addonArgs.Append(" ");
                //addonArgs.Append(Global.CommandContextString);
                //addonArgs.Append(Global.CommandStringRackAndServerVirtualize);
                //rackMenuItemVirtualize.AddOnName = "QueueMarkerEvent";
                //rackMenuItemVirtualize.AddOnArgs = addonArgs.ToString();


                // Add the status menu Item to the menu
                rackMenuItemStatusWindow = menuItems.Add();
                rackMenuItemStatusWindow.Caption = Global.GetResourceString("MenuStatusWindow");
                rackMenuItemStatusWindow.State = (short)Visio.VisUIButtonState.visButtonUp;
                rackMenuItemStatusWindow.BeginGroup = true;
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringShowStatusWindow);
                rackMenuItemStatusWindow.AddOnName = "QueueMarkerEvent";
                rackMenuItemStatusWindow.AddOnArgs = addonArgs.ToString();

                // Add the help menu Item to the menu
                rackMenuItemHelp = menuItems.Add();
                rackMenuItemHelp.Caption = Global.GetResourceString("MenuHelp");
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringHelp);
                rackMenuItemHelp.AddOnName = "QueueMarkerEvent";
                rackMenuItemHelp.AddOnArgs = addonArgs.ToString();
                rackMenuItemHelp.BeginGroup = true;

                // Add the about menu Item to the menu
                rackMenuItemAbout = menuItems.Add();
                rackMenuItemAbout.Caption = Global.GetResourceString("MenuAbout");
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringAbout);
                rackMenuItemAbout.AddOnName = "QueueMarkerEvent";
                rackMenuItemAbout.AddOnArgs = addonArgs.ToString();
                rackMenuItemAbout.BeginGroup = true;

                rackMenuItemConfigure.FaceID = 122;
                rackMenuItemImportData.FaceID = 120;
                rackMenuItemAnalyze.FaceID = 106;
                rackMenuItemVirtualize.FaceID = 63;
                rackMenuItemHelp.FaceID = 133;
                   
                AddinMenu_UIBuilder_RefreshUIObject(Global.MenuStateDocumentOpen);

                // Set the custom menu to the active document
                addinDocument.VisioDocument.SetCustomMenus(uiObject);
                uiObject.UpdateUI();
            }
            catch (RackException ex)
            {
                // Exception from sub function. Pass on to entry point
                throw new RackException("Addin build menu failed", ex.InnerException);
            }
            catch (Exception ex)
            {
                throw new RackException("Addin build menu failed", ex);
            }
        }

        /// <summary>
        /// Function to add the addin toolbar to Visio Document
        /// </summary>
        public void CreateToolbar(Document addinDocument)
        {
            System.Text.StringBuilder addonArgs = null;

            uiToolbarObject = null;

            try
            {
                // Get the document custom toolbar UIObject
                if (addinDocument.VisioDocument.CustomToolbars != null)
                {
                    uiToolbarObject = addinDocument.VisioDocument.CustomToolbars.Clone;
                }
                else if (addinDocument.VisioDocument.Application.CustomToolbars != null)
                {
                    // Get the application custom toolbar UIObject
                    uiToolbarObject = addinDocument.VisioDocument.Application.CustomToolbars.Clone;
                }

                if (uiToolbarObject == null)
                {
                    // No custom toolbar object found
                    // Load from default toolbar file
                    if (System.IO.File.Exists(Global.DefaultToolbar))
                    {
                        // Set customer toolbar file
                        addinDocument.VisioDocument.Application.CustomToolbarsFile = Global.DefaultToolbar;

                        // Get the application custom toolbar UIObject
                        if (addinDocument.VisioDocument.Application.CustomToolbars != null)
                        {
                            uiToolbarObject = addinDocument.VisioDocument.Application.CustomToolbars.Clone;
                        }

                        // Reset the custom toolbar file
                        addinDocument.VisioDocument.Application.CustomToolbarsFile = "";
                    }
                    if (uiToolbarObject == null)
                    {
                        // Toolbar add failed
                        return;
                    }
                }

                // Get the toolbar sets of the visio application
                Visio.ToolbarSets toolbarSets = uiToolbarObject.ToolbarSets;
                // Get the item Id of the drawing object
                short itemId = (short)Visio.VisUIObjSets.visUIObjSetDrawing;
                // Get the toolbar set of drawing object
                Visio.ToolbarSet toolbarSet = toolbarSets.get_ItemAtID(itemId);
                // Get the toolbars from toolbar set
                Visio.Toolbars toolbars = toolbarSet.Toolbars;

                // Add the toolbar at a position & set the caption
                rackServerToolbar = toolbars.AddAt(toolbars.Count - 1);
                rackServerToolbar.Caption = Global.GetResourceString("ToolbarName");

                // Set the toolbar items to the toolbar
                Visio.ToolbarItems toolbarItems = rackServerToolbar.ToolbarItems;

                // Add the configure toolbar Item to the toolbar
                Visio.ToolbarItem rackToolbarItemConfigure = toolbarItems.Add();
                rackToolbarItemConfigure.Caption = Global.GetResourceString("MenuConfigure");
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringConfigure);
                rackToolbarItemConfigure.AddOnName = "QueueMarkerEvent";
                rackToolbarItemConfigure.AddOnArgs = addonArgs.ToString();
                rackToolbarItemConfigure.FaceID = 122;

                // Add Import data toolbar item
                Visio.ToolbarItem rackToolbarItemImportData = toolbarItems.Add();
                rackToolbarItemImportData.Caption = Global.GetResourceString("MenuImportData");
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringImportData);
                rackToolbarItemImportData.AddOnName = "QueueMarkerEvent";
                rackToolbarItemImportData.AddOnArgs = addonArgs.ToString();
                rackToolbarItemImportData.FaceID = 120;

                // Add the analyze toolbar Item to the toolbar
                Visio.ToolbarItem rackToolbarItemAnalyze = toolbarItems.Add();
                rackToolbarItemAnalyze.Caption = Global.GetResourceString("MenuAnalyze");
                rackToolbarItemAnalyze.State = (short)Visio.VisUIButtonState.visButtonUp;
                rackToolbarItemAnalyze.BeginGroup = true;
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringAnalyze);
                rackToolbarItemAnalyze.AddOnName = "QueueMarkerEvent";
                rackToolbarItemAnalyze.AddOnArgs = addonArgs.ToString();
                rackToolbarItemAnalyze.FaceID = 106;

                // Add the virtualize toolbar Item to the toolbar
                Visio.ToolbarItem rackToolbarItemVirtualize = toolbarItems.Add();
                rackToolbarItemVirtualize.Caption = Global.GetResourceString("MenuVirtualize");
                rackToolbarItemVirtualize.State = (short)Visio.VisUIButtonState.visButtonUp;
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringVirtualize);
                rackToolbarItemVirtualize.AddOnName = "QueueMarkerEvent";
                rackToolbarItemVirtualize.AddOnArgs = addonArgs.ToString();
                rackToolbarItemVirtualize.FaceID = 63;

                // Add the help toolbar Item to the toolbar
                Visio.ToolbarItem rackToolbarItemHelp = toolbarItems.Add();
                rackToolbarItemHelp.Caption = Global.GetResourceString("MenuHelp");
                addonArgs = new System.Text.StringBuilder();
                addonArgs.Append(Global.DocumentContextString);
                addonArgs.Append(addinDocument.VisioDocument.ID);
                addonArgs.Append(" ");
                addonArgs.Append(Global.ApplicationContextString);
                addonArgs.Append(Global.ApplicationId);
                addonArgs.Append(" ");
                addonArgs.Append(Global.CommandContextString);
                addonArgs.Append(Global.CommandStringHelp);
                rackToolbarItemHelp.AddOnName = "QueueMarkerEvent";
                rackToolbarItemHelp.AddOnArgs = addonArgs.ToString();
                rackToolbarItemHelp.BeginGroup = true;
                rackToolbarItemHelp.FaceID = 133;

                // Set the custom toolbar to the active document
                addinDocument.VisioDocument.SetCustomToolbars(uiToolbarObject);
                uiToolbarObject.UpdateUI();
            }
            catch (RackException ex)
            {
                // Exception from sub function. Pass on to entry point
                throw new RackException("Addin build toolbar failed", ex.InnerException);
            }
            catch (Exception ex)
            {
                throw new RackException("Addin build toolbar failed", ex);
            }
        }

        /// <summary>
        /// Update the menu status based on the state of the document
        /// </summary>
        /// <param name="menuState"></param>
        internal void AddinMenu_UIBuilder_RefreshUIObject(string menuState)
        {
            try
            {
                switch (menuState)
                {
                    case Global.MenuStateDocumentOpen:
                        rackMenuItemConfigure.Enabled = true;
                        rackMenuItemImportData.Enabled = true;
                        rackMenuItemAnalyze.Enabled = true;
                        rackMenuItemVirtualize.Enabled = true;
                        rackMenuItemStatusWindow.Enabled = true;
                        rackMenuItemHelp.Enabled = true;
                        break;

                    case Global.MenuStateSetStatus:
                        rackMenuItemStatusWindow.State = (short)Visio.VisUIButtonState.visButtonDown;
                        break;

                    case Global.MenuStateResetStatus:
                        rackMenuItemStatusWindow.State = (short)Visio.VisUIButtonState.visButtonUp;
                        break;

                    case Global.MenuStateToggleStatus:
                        ToggleMenuItemState(rackMenuItemStatusWindow);
                        break;

                    default:
                        break;
                }
                uiObject.UpdateUI();
            }
            catch (RackException ex)
            {
                // Exception from sub function. Pass on to entry point
                throw new RackException("Update UI", ex.InnerException);
            }
            catch (Exception ex)
            {
                // Exception from current function
                throw new RackException("Update UI", ex);
            }
            return;
        }      

        /// <summary>
        /// Function to remove the addin menu from Visio
        /// </summary>
        public void RemoveAddinMenu()
        {
            // Remove addin menu
            if (rackServerMenu != null)
            {
                rackServerMenu.Delete();
                rackServerMenu = null;
                if (uiObject != null)
                {
                    uiObject.UpdateUI();
                }
            }
        }

        /// <summary>
        /// Function to remove the addin toolbar from Visio
        /// </summary>
        public void RemoveAddinToolbar()
        {
            // Remove addin menu
            if (rackServerToolbar != null)
            {
                rackServerToolbar.Delete();
                rackServerToolbar = null;
                if (uiToolbarObject != null)
                {
                    uiToolbarObject.UpdateUI();
                }
            }
        }

        /// <summary>
        /// Toggle state of menu item
        /// </summary>
        /// <param name="menuItem"></param>
        internal void ToggleMenuItemState(Visio.MenuItem menuItem)
        {
            if (menuItem.State == (short)Visio.VisUIButtonState.visButtonDown)
            {
                menuItem.State = (short)Visio.VisUIButtonState.visButtonUp;
            }
            else
            {
                menuItem.State = (short)Visio.VisUIButtonState.visButtonDown;
            }
            // Refresh menu object
            uiObject.UpdateUI();
        }

        /// <summary>
        /// Toggle window visibility
        /// </summary>
        /// <param name="currWindow"></param>
        internal static void ToggleWindowState(AddonWindowWrapper currWindow)
        {
            // toggle the state of this button
            if (currWindow.Visible == true)
            {
                currWindow.Visible = false;
            }
            else
            {
                currWindow.Visible = true;
            }
        }

        #endregion
    }
}
